<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <link rel="stylesheet" href="http://cdn.staticfile.org/cesium/1.69.0/Widgets/widgets.css">
    <link rel="stylesheet" href="../css//index.css">
</head>

<body>
    <div class="fun-menu">
        <button onclick="switchBasemap(true)">天地图-影像</button>
        <button onclick="switchBasemap(false)">移除</button>
        <button onclick="flyTo()">飞到图层</button>
    </div>
    <div id=cesiumContainer></div>

    <script src="http://cdn.staticfile.org/cesium/1.69.0/Cesium.js"></script>
    <script src="../libs/cpc/index.js"></script>
    <script>
        let imageLayer = null;
        const map = new cpc.Map({
            container: "cesiumContainer",
            baseMaps: [
                "http://map.geoq.cn/arcgis/rest/services/ChinaOnlineStreetPurplishBlue/MapServer",
            ]
        });

        function switchBasemap(isAdd) {
            /**
             * 和type底图一致，直接new 对象即可
             *    底图： {
             type: "TDTImageryLayer",
             mold: "image",
             tk: "7b435c61bff7f77eb49206e10d6397bd",
            }
             */
            if (isAdd) {
                imageLayer = new cpc.TDTImageryLayer({
                    mold: "image",
                    tk: "7b435c61bff7f77eb49206e10d6397bd",
                })
                imageLayer.when((cesiumObj) => {
                })
                map.add(imageLayer);//添加到地图上的方式有两种---1
                // imageLayer.addToMap(map);//添加到地图上的方式有两种---2
            } else {
                map.remove(imageLayer);//移除也有两种------1
                // imageLayer.removeByMap();//移除也有两种------2

                imageLayer = null;
            }
        }
        function flyTo() {
            if(!imageLayer){
                alert("请先添加图层");
                return;
            }
            imageLayer.flyTo();
        }
    </script>
</body>

</html>